/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/

package com.ewerp.mud.core;

import java.io.FileInputStream;
import java.util.Properties;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.ewerp.mud.core.exceptions.EwerpMudException;
import com.ewerp.mud.core.plugin.IPlugin;
import com.ewerp.mud.core.properties.IProperties;
import com.ewerp.mud.lifecycle.ILifecycleListener;

public class EwerpProperties extends Properties implements IPlugin, IProperties, ILifecycleListener {
    private static final long serialVersionUID = 1L;
    
    protected static EwerpProperties instance = null;
    
    public static EwerpProperties getInstance() throws EwerpMudException {
        if(instance == null) {
            instance = new EwerpProperties();
            instance.init();
        }
        
        return instance;
    }
    
    protected EwerpProperties() {
    }
    
    @Override
	public Class<?>[] getInterfaces() {
		return new Class<?>[] { IProperties.class };
	}

    @Override
    public void init() {
    	Options options = null;
    	
        try {
        	if(EwerpMud.commandLineArgs != null) {
		    	// Parse command line
		    	options = new Options();
		    	
		    	// Add configuration file option
		    	OptionBuilder.withArgName( "file" );
		    	OptionBuilder.withLongOpt("config");
		    	OptionBuilder.hasArg();
		        OptionBuilder.withDescription("use a given configuration file");
		    	options.addOption(OptionBuilder.create('c'));
		    	
		    	// Add help option
		    	options.addOption("h", "help", false, "print this help message");
		    	
		    	CommandLineParser parser = new GnuParser();

		        CommandLine cmdLineArgs = parser.parse( options, EwerpMud.commandLineArgs);
		        
		        if(cmdLineArgs.hasOption("help")) {
		        	throw new ParseException("Display help message");
		        }
        	}
        	
        	// Set system defaults
        	setProperty("file.command-registry", "res/command.registry.xml");
        	
            load(new FileInputStream("res/ewerp.mud.properties"));
        } catch (ParseException e) {
        	if(options != null) {
	        	HelpFormatter formatter = new HelpFormatter();
	        	formatter.printHelp( "ejmud [options]", options );
        	}
        	System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void pause() {
    }

    @Override
    public void start() {
    }

    @Override
    public void stop() {
    }
}
